package com.un.ebs.production.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.un.ebs.core.util.StringUtils;
import com.un.ebs.core.util.WebResult;
import com.un.ebs.core.util.WebUtils;
import com.un.ebs.production.domain.ProductionSeqExt;
import com.un.ebs.production.domain.ProductionSeqItemExt;
import com.un.ebs.production.domain.ProductionSeqItemView;
import com.un.ebs.production.dto.ProductionSeqDto;
import com.un.ebs.production.dto.ProductionSeqItemDto;
import com.un.ebs.production.service.ProductionSeqService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * (ProductionSeq)表控制层
 *
 * @author liubo
 * @since 2020-07-06 16:27:16
 */
@RestController
@RequestMapping("productionSeq")
@Validated
@Api(value = "production-seq")
public class ProductionSeqController {
    /**
     * 服务对象
     */
    @Resource
    private ProductionSeqService service;

    @PostMapping("update")
    @ApiOperation(value = "修改")
    public WebResult update(@RequestBody @Validated ProductionSeqDto req) {
        service.update(req);
        return WebUtils.success();
    }

    @PostMapping("addSeq")
    @ApiOperation(value = "添加工艺")
    public WebResult addSeq(@RequestParam String name) {
        service.addSeq(name);
        return WebUtils.success();
    }

    @PostMapping("udpateSeq")
    @ApiOperation(value = "添加或修改工艺")
    public WebResult addSeq(@RequestParam String id, @RequestParam String name) {
        service.updateSeq(id, name);
        return WebUtils.success();
    }

    @PostMapping("addSeqItem")
    @ApiOperation(value = "添加工艺工序")
    public WebResult addSeqItem(@RequestBody @Validated ProductionSeqItemDto req) {
        service.addSeqItem(req);
        return WebUtils.success();
    }

    @PostMapping("updateSeqItem")
    @ApiOperation(value = "修改工艺工序")
    public WebResult updateSeqItem(@RequestBody @Validated ProductionSeqItemDto req) {
        service.updateSeqItem(req);
        return WebUtils.success();
    }

    @PostMapping("copyTo")
    @ApiOperation(value = "复制为")
    public WebResult copyTo(@RequestParam String seqId, @RequestParam String newName) {
        service.copyTo(seqId, newName);
        return WebUtils.success();
    }

    @PostMapping("delete")
    @ApiOperation(value = "删除")
    public WebResult delete(@RequestParam String id) {
        service.delete(id);
        return WebUtils.success();
    }

    @PostMapping("deleteItem")
    @ApiOperation(value = "删除")
    public WebResult deleteItem(@RequestParam String itemId) {
        service.deleteItem(itemId);
        return WebUtils.success();
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "详情，工序集")
    public WebResult<ProductionSeqExt> queryById(@PathVariable String id) {
        return WebUtils.success(service.queryById(id));
    }

    @GetMapping("/item/{id}")
    @ApiOperation(value = "详情，工序")
    public WebResult<ProductionSeqItemView> queryItemById(@PathVariable String id) {
        return WebUtils.success(service.queryItemById(id));
    }

    @GetMapping("listItem")
    @ApiOperation(value = "详情，工序")
    public WebResult<IPage<ProductionSeqItemView>> queryItemViewById(@RequestParam String seqId) {
        return WebUtils.success(service.queryItemViewBySeq(seqId, -1, 1));
    }

    @GetMapping("queryItem")
    @ApiOperation(value = "查询工艺下的所有工序")
    public WebResult<IPage<ProductionSeqItemExt>> queryItem(@RequestParam String id, @RequestParam int pageSize, @RequestParam int pageIndex) {
        return WebUtils.success(service.queryItem(id, pageSize, pageIndex));
    }

    @GetMapping("listItemViewById")
    @ApiOperation(value = "查询工艺下的所有工序")
    public WebResult<IPage<ProductionSeqItemExt>> queryItemViewBy(@RequestParam String seqId, @RequestParam int pageSize, @RequestParam int pageIndex) {
        return WebUtils.success(service.queryItemView(seqId, pageSize, pageIndex));
    }

    @GetMapping("list")
    @ApiOperation(value = "查询工艺")
    public WebResult<IPage<ProductionSeqExt>> query(@RequestParam String searchText, @RequestParam int pageSize, @RequestParam int pageIndex) {
        return WebUtils.success(service.query(StringUtils.isNullOrEmpty(searchText) ? null : searchText.trim(), pageSize, pageIndex));
    }
}